/***************************************************************************
 *   Copyright (C) 2009-2010 by Lasse Liehu                                *
 *   lliehu@kolumbus.fi                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "win32loginoperation.h"

#include <QtDebug>

#define WIN32_LEAN_AND_MEAN // excluding some rarely used things
#include <Windows.h>

#include "../googlelogindata.h"
#include "win32loginworker.h"

//BEGIN Win32LoginOperationPrivate
class Win32LoginOperationPrivate
{
    public:
        Win32LoginOperationPrivate(GoogleLoginData& loginData)
        : worker(loginData)
        {
        }

        Win32LoginWorker worker;
        bool loginCompleted;
};
//END Win32LoginOperationPrivate

Win32LoginOperation::Win32LoginOperation(GoogleLoginData& loginData,
                                         QObject *parent)
: LoginOperation(loginData, parent),
  d(new Win32LoginOperationPrivate(loginData) )
{
    d->loginCompleted = false;

    // forward worker's signals to actual LoginOperation
    connect(&d->worker, SIGNAL(updateStatus(Login::Status)), this,
            SLOT(slotUpdateStatus(Login::Status) ));
    connect(&d->worker, SIGNAL(setConclusion(Login::Conclusion,
            const QString&)), this, SIGNAL(setConclusion(Login::Conclusion,
                                                         const QString&) ));
    connect(&d->worker, SIGNAL(finished()), this, SLOT(slotWorkerFinished()));
}

Win32LoginOperation::~Win32LoginOperation()
{
    delete d;
}

void Win32LoginOperation::start()
{
    d->worker.start();
}

void Win32LoginOperation::slotUpdateStatus(Login::Status newStatus)
{
    if (newStatus == Login::Completed)
    {
        d->loginCompleted = true;
        // slotWorkerFinished() should be called soon,
        // it will emit signal updateStatus
    }
    else emit updateStatus(newStatus);
}

void Win32LoginOperation::slotWorkerFinished()
{
    if (d->loginCompleted) emit updateStatus(Login::Completed);
}
